﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Cms.Model;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;


namespace Portal.Cms.Bussiness
{
    public class RelationTypeProcessBll
    {
        public static RelationTypeCls CreateRelationTypeModel(string SiteId, string RelationTypeId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableRelationType " +
                                               "     where RelationTypeId=@RelationTypeId order by SortIndex",
                    new DbParam[] 
                    { 
                        new DbParam("RelationTypeId", RelationTypeId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            RelationTypeCls ORelationType = RelationTypeParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ORelationType;
        }


        public static RelationTypeCls[] ReadingAllRelationType(string SiteId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableRelationType order by SortIndex", null);
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new RelationTypeCls[0];
            }
            RelationTypeCls[] RelationType = RelationTypeParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return RelationType;
        }

        public static RelationTypeCls[] ReadingRelationType(string SiteId, string GroupDataId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableRelationType where frkGroupDataId=@GroupDataId order by SortIndex",
                    new DbParam[] { new DbParam("GroupDataId", GroupDataId) });

            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new RelationTypeCls[0];
            }

            RelationTypeCls[] RelationType = RelationTypeParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return RelationType;
        }

        public static RelationTypeCls[] ReadingRelationTypeOut(string SiteId, string GroupDataId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                //DBService.GetDataSet(null, "select * from TableRelationType where InOut=1 and frkGroupDataId='" + GroupDataId + "' order by SortIndex", null);
            DBService.GetDataSet(null, "select * from TableRelationType where InOut=@InOut and frkGroupDataId=@GroupDataId order by SortIndex",
                                new DbParam[] { new DbParam("InOut",1),
                                                new DbParam("GroupDataId",GroupDataId)});
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new RelationTypeCls[0];
            }

            RelationTypeCls[] RelationType = RelationTypeParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return RelationType;
        }

        public static void AddRelationType(string SiteId, string GroupDataId, string RelationTypeName, string SortIndex, string InOut)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string RelationTypeId = System.Guid.NewGuid().ToString();
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Insert(null, "TableRelationType",
                    new DbParam[] 
                    { 
                        new DbParam("RelationTypeId", RelationTypeId),
                        new DbParam("frkGroupDataId", GroupDataId),
                        new DbParam("RelationTypeName", RelationTypeName),
                        new DbParam("SortIndex", SortIndex),
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("DataLanguage", DataLanguage),
                        new DbParam("InOut", InOut),
                        
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Thêm mới loại quan hệ " + RelationTypeName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void SaveRelationType(string SiteId, string RelationTypeId, string RelationTypeName, string SortIndex, string InOut)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Update(null, "TableRelationType", "RelationTypeId", RelationTypeId,
                    new DbParam[] 
                    { 
                        new DbParam("RelationTypeName", RelationTypeName),
                        new DbParam("SortIndex", SortIndex),
                        new DbParam("InOut", InOut),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Sửa chữa loại quan hệ " + RelationTypeName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void DeleteRelationType(string SiteId, string RelationTypeId)
        {
            RelationTypeCls ORelationType = RelationTypeProcessBll.CreateRelationTypeModel(SiteId, RelationTypeId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableRelationType where RelationTypeId=@RelationTypeId; ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("RelationTypeId", RelationTypeId),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Xóa loại quan hệ " + ORelationType.RelationTypeName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }
    }
}
